//package com.tiancheng.trade.settlement.util.queue;
//
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.BeanFactory;
//import org.springframework.beans.factory.BeanFactoryAware;
//import org.springframework.beans.factory.DisposableBean;
//import org.springframework.beans.factory.InitializingBean;
//
//import java.util.Random;
//import java.util.TreeMap;
//
///**
// * @Author: likailun
// * @Description:
// * @Date: create in 2022/12/26 5:33 下午
// */
//public abstract class AbstractCustomQueueConsumer implements BeanFactoryAware, InitializingBean, DisposableBean, RedisDelayQueueHandle {
//
//    private BeanFactory beanFactory;
//    /**
//     * 连接池是否关闭
//     */
//    volatile boolean isCloseConnectionPool = false;
//    private RedisDelayQueueUtil redisDelayQueueUtil;
//
//    @Override
//    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
//        this.beanFactory = beanFactory;
//    }
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        this.redisDelayQueueUtil = this.beanFactory.getBean(RedisDelayQueueUtil.class);
//        String queueName = this.getQueueName();
//        if (queueName == null || queueName.isEmpty()) {
//            throw new IllegalArgumentException("队列名称不能为空");
//        }
//        Integer pollingWaitSeconds = this.getPullIntervalSeconds();
//        if (pollingWaitSeconds != null) {
//            if (pollingWaitSeconds < 1 || pollingWaitSeconds > 30) {
//                throw new IllegalArgumentException("pullIntervalSeconds取值范围为0s到30s");
//            }
//        }
//        if (this.getHandleThreadNum() <= 0) {
//            throw new IllegalArgumentException("消息处理线程数必须大于0");
//        }
//
//        MessageFetch messageFetch = new MessageFetch();
//        for (int i = 0; i < this.getHandleThreadNum(); i++) {
//            Thread thread = new Thread(messageFetch);
//            thread.setName("redis-queueConsumer-" + this.getQueueName() + i);
//            thread.start();
//        }
//    }
//
//    @Override
//    public void destroy() throws Exception {
//        this.isCloseConnectionPool = true;
//    }
//
//
//    ;
//
//    private class MessageFetch implements Runnable {
//        private final Logger logger = LoggerFactory.getLogger(MessageFetch.class);
//        private TreeMap<String, Object> fetchParam;
//        private String queueName;
//
//        MessageFetch() {
//            this.queueName = AbstractCustomQueueConsumer.this.getQueueName();
//            this.fetchParam = new TreeMap<>();
//            this.fetchParam.put("queueName", this.queueName);
//            if (AbstractCustomQueueConsumer.this.getPullIntervalSeconds() != null) {
//                this.fetchParam.put("pullIntervalSeconds", AbstractCustomQueueConsumer.this.getPullIntervalSeconds());
//            }
//        }
//
//        @Override
//        public void run() {
//            Random roRandom = new Random();
//            // 连接没被关闭的情况下进行循环
//            while (!Thread.currentThread().isInterrupted() && !AbstractCustomQueueConsumer.this.isCloseConnectionPool) {
//                try {
//                    Thread.sleep(AbstractCustomQueueConsumer.this.getPullIntervalSeconds() * 1000);
//                } catch (InterruptedException e) {
//                    Thread.currentThread().interrupt();
//                    e.printStackTrace();
//                }
//                Object m = this.fetch();
//                if (m != null) {
//                    try {
//                        logger.info("拉取queue：{}消息",AbstractCustomQueueConsumer.this.getQueueName());
//                        AbstractCustomQueueConsumer.this.execute(m);
//                    } catch (Exception e) {
//                        this.logger.error("redis queue message handle failure", e);
//                    }
//                }
//            }
//        }
//
//        private Object fetch() {
//            try {
//                return redisDelayQueueUtil.getDelayQueue(this.queueName);
//            } catch (Exception e) {
//                this.logger.error("redis queue message fetch failure", e);
//                return null;
//            }
//        }
//    }
//
//    /**
//     * 判断实例是否是UP状态
//     */
//    boolean instanceIsUp() {
//        boolean result = true;
//        if (this.beanFactory.containsBean("eurekaClient")) {
////            EurekaClient eurekaClient = this.beanFactory.getBean(EurekaClient.class);
////            //如果实例的本地状态或者在eureka的状态不是up,则认为不可执行
////            try {
////                if (eurekaClient.getApplicationInfoManager().getInfo().getStatus() != InstanceInfo.InstanceStatus.UP || eurekaClient.getInstanceRemoteStatus() != InstanceInfo.InstanceStatus.UP) {
////                    result = false;
////                }
////            } catch (Exception e) {
////                result = false;
////            }
//        }
//        return result;
//    }
//}
